package com.gagakuai.bpm.config.busniess;

import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;


/*
 * 消息启动子流程
 * 信号启动子流程
 */
public class InstantiateProcessByMessageDelegate implements JavaDelegate {

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        try {
            String type = (String) execution.getVariable("type");
            RuntimeService runtimeService = execution.getProcessEngineServices().getRuntimeService();

            switch (type) {
                case "tel":
                    runtimeService.startProcessInstanceByMessage("telMessage", execution.getVariables());
                    break;
                case "sign":
                    // 发送信号
                    runtimeService.createSignalEvent("signal").setVariables(execution.getVariables()).send();
                    break;
                default:
                    runtimeService.startProcessInstanceByMessage("pcMessage", execution.getVariables());
                    break;
            }
        }catch (Exception e){
            throw new Exception("启动子流程异常：{}", e);
        }
    }
}
